<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('业务类型')" prop="busType">
            <el-select v-model="form.busType" value="" :placeholder="$t('请选择')" filterable clearable @change="busTypeChange">
              <el-option
                v-for="dict in wfBusFlowTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item v-if="form.busType !== 'spv'" :label="$t('业务数据类型')" prop="busDataType">
            <el-select v-model="form.busDataType" value="" :placeholder="$t('请选择')" filterable clearable>
              <el-option
                v-for="dict in wfBusDataTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('所属部门')" prop="deptId">
            <DeptSelect v-model="form.deptId" :placeholder="$t('所属部门')" check-strictly="true" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('可以提交下一阶段的角色')" prop="roleId">
            <role-select v-model="form.roleId" :placeholder="$t('可以提交下一阶段的角色')" multiple />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="autoUpdateNode">
            <span slot="label">
              {{ $t('流程阶段是否自动更新') }}
              <el-tooltip :content="$t('启动流程二级节点的流程,自动更新一级节点')" placement="top">
                <i class="el-icon-question" />
              </el-tooltip>
            </span>
            <dictSelect
              v-model="form.autoUpdateNode"
              dictionary="sys_yes_no"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <SaveBtn has-permi="act:busflow" />
    </div>
  </div>
</template>

<script>
    import { treeselect } from '@/api/system/dept'
    import { saveWfConfigFlow, getWfConfigFlow } from '@/api/act/busFlow/wfConfigFlow'
    import RoleSelect from '@/components/UserSelect/role'
    export default {
      components: {
        RoleSelect
      },
        data() {
            return {
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                  busType: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
                },
                // 业务类型字典
                wfBusFlowTypeOptions: [],
                // 业务数据类型字典
                wfBusDataTypeOptions: [],
                // 部门树选项
                deptOptions: [],
                // 遮罩层
                loading: false
            }
        },
        created() {
          this.getDicts('wf_bus_flow_type').then(response => {
            this.wfBusFlowTypeOptions = response.data
            this.busTypeChange(this.form.busType)
          })
          // 查询部门下拉框
          this.getTreeselect()
        },
        methods: {
            // 初始化数据
            init(id) {
                this.loading = true
                if (id != null && id !== '') {
                    getWfConfigFlow(id).then(response => {
                        var obj = JSON.parse(JSON.stringify(response.data))
                      obj.roleId = JSON.parse(response.data.roleId)
                        this.form = obj
                        this.busTypeChange(this.form.busType)
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                busType: undefined,
                version: undefined,
                autoUpdateNode: 'N',
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            /** 查询部门下拉树结构 */
            getTreeselect() {
              treeselect().then(response => {
                this.deptOptions = response.data
              })
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                      this.loading = true
                      if (this.form.busType === 'spv') {
                        this.form.busDataType = undefined
                      }
                      var obj = JSON.parse(JSON.stringify(this.form))
                      obj.roleId = JSON.stringify(this.form.roleId)
                        saveWfConfigFlow(obj).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.$parent.$parent.formOpen = false
                                this.$parent.$parent.getList()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
            busTypeChange(busType) {
              Object.keys(this.wfBusFlowTypeOptions).map((key) => {
                if (this.wfBusFlowTypeOptions[key].dictValue === ('' + busType)) {
                  this.getDicts(this.wfBusFlowTypeOptions[key].childDict).then(response => {
                    this.wfBusDataTypeOptions = response.data
                  })
                  return false
                }
              })
            }

        }

    }
</script>
